import type { AppRouteRecordRaw, AppRouteModule } from '/@/router/types';

import { PAGE_NOT_FOUND_ROUTE, REDIRECT_ROUTE } from '/@/router/routes/basic';

import { mainOutRoutes } from './mainOut';
import { PageEnum } from '/@/enums/pageEnum';
import { t } from '/@/hooks/web/useI18n';
import { AUTH_LAYOUT } from '../constant';

const modules = import.meta.glob('./modules/**/*.ts', { eager: true });

const routeModuleList: AppRouteModule[] = [];

Object.keys(modules).forEach((key) => {
  const mod = (modules as Recordable)[key].default || {};
  const modList = Array.isArray(mod) ? [...mod] : [mod];
  routeModuleList.push(...modList);
});

export const asyncRoutes = [PAGE_NOT_FOUND_ROUTE, ...routeModuleList];

export const RootRoute: AppRouteRecordRaw = {
  path: '/',
  name: 'Root',
  redirect: PageEnum.BASE_LOGIN,
  meta: {
    title: 'Root',
  },
};

// export const LoginRoute: AppRouteRecordRaw = {
//   path: '/login',
//   name: 'Login',
//   component: () => import('/@/layouts/views/login/Login.vue'),
//   meta: {
//     title: t('routes.basic.login'),
//   },
// };

const ModPwdRoute: AppRouteModule = {
  path: '/modPwd',
  name: 'ModPwd',
  component: () => import('/@/layouts/views/account/modPwd.vue'),
  meta: {
    icon: 'i-ant-design:key-outlined',
    title: t('sys.account.modifyPwd'),
  },
};

export const LoginRoute: AppRouteModule = {
  component: AUTH_LAYOUT,
  meta: {
    hideInTab: true,
    title: 'Authentication',
  },
  name: 'Authentication',
  path: '/auth',
  redirect: PageEnum.BASE_LOGIN,
  children: [
    {
      name: 'Login',
      path: '/auth/login',
      component: () => import('/@/layouts/views/login/login.vue'),
      meta: {
        title: t('routes.basic.login'),
      },
    },
    {
      name: 'CodeLogin',
      path: '/auth/code-login',
      component: () => import('/@/layouts/views/login/codeLogin.vue'),
      meta: {
        title: t('routes.basic.codeLogin'),
      },
    },
    {
      name: 'QrCodeLogin',
      path: '/auth/qrcode-login',
      component: () => import('/@/layouts/views/login/qrcodeLogin.vue'),
      meta: {
        title: t('routes.basic.qrcodeLogin'),
      },
    },
    {
      name: 'ForgetPassword',
      path: '/auth/forget-password',
      component: () => import('/@/layouts/views/login/forgetPassword.vue'),
      meta: {
        title: t('routes.basic.forgetPassword'),
      },
    },
    {
      name: 'Register',
      path: '/auth/register',
      component: () => import('/@/layouts/views/login/register.vue'),
      meta: {
        title: t('routes.basic.register'),
      },
    },
    {
      name: 'CreatePassword',
      path: '/auth/create-password',
      component: () => import('/@/layouts/views/login/createPassword.vue'),
      meta: {
        title: t('routes.basic.createPassword'),
      },
    },
  ],
};

// Basic routing without permission
export const basicRoutes = [LoginRoute, ModPwdRoute, RootRoute, ...mainOutRoutes, REDIRECT_ROUTE, PAGE_NOT_FOUND_ROUTE];
